<?php

namespace seecms\region\controller;

use seecms\controller\Resource;
use seecms\lib\builder\query\TextQuery;
use seecms\lib\Transfer;
use seecms\region\lib\RegionQuery;
use seecms\See;

class Region extends Resource
{
    protected $table_name = 'see_region';

    protected $dictionary_name = 'region';

    protected $template = 'region/index';

    protected function pageTitle(): string
    {
        return __('region.page_title');
    }

    /**
     * @return array
     */
    protected function queryExpress(): array
    {
        $query = $this->query();

        $params = See::request()->get() ?: [];
        $where = Transfer::pageQuery($query, $params);

        if ($params['province_code'] ?? '') {
            $where['parent_code'] = $params['province_code'];
        }

        if ($params['city_code'] ?? '') {
            $where['parent_code'] = $params['city_code'];
        }

        return $where;
    }

    /**
     * @return array
     */
    public function query(): array
    {
        return [
            RegionQuery::init()->setLevel(3),
            'code',
            TextQuery::init('name')->setExpress('like'),
        ];
    }

    public function columns(): array
    {
        return [
            'id',
            'code',
            'parent_code',
            'name',
            'pinyin',
            'sort',
            'status',
            'created_at',
        ];
    }

    public function controls(): array
    {
        return [
            'id',
            'code',
            'parent_code',
            'name',
            'pinyin',
            'first_char',
            'level',
            'sort',
            'status',
        ];
    }

}